package com.xiaoyi.intentsdklibrary.SDK.Event;

import android.Manifest;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.support.v4.app.ActivityCompat;

import com.xiaoyi.intentsdklibrary.KeyClickBean;
import com.xiaoyi.intentsdklibrary.NoticTextBean;
import com.xiaoyi.intentsdklibrary.ScreenTextBean;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class EvenSDK {
    public static boolean isListenNoticText = false;
    public static boolean isListenScreenText = false;
    public static OnNoticTextListener mOnNoticTextListener;
    public static OnKeyClickListener onKeyClickListener;
    private static final EvenSDK ourInstance = new EvenSDK();
    private BroadcastReceiver mBlueToothBroadcastReceiver;
    private Context mContexPhoneCall;
    private Context mContextBluetooth;
    private Context mContextNetWork;
    private Context mContextScreen;
    private SensorManager mManager;
    private BroadcastReceiver mNewWrokBroadcastReceiver;
    private BroadcastReceiver mOnPhoneCallBroadcastReceiver;
    private OnScreenTextListener mOnScreenTextListener;
    private BroadcastReceiver mScreenBroadcastReceiver;
    private SensorEventListener mSensorEventListener;

    public enum BlueToothType {
        BLUETOOTH_ON,
        BLUETOOTH_OFF,
        BLUETOOTH_CONNECTED,
        BLUETOOTH_DISCONNECTED
    }

    public enum NetworkType {
        NETWORK_WIFI,
        NETWORK_DATA,
        NETWORK_DISCONNECT
    }

    public interface OnKeyClickListener {
        void onResult(KeyClickBean keyClickBean);
    }

    public interface OnNoticTextListener {
        void onResult(NoticTextBean noticTextBean);
    }

    public interface OnScreenTextListener {
        void onResult(ScreenTextBean screenTextBean);
    }

    public enum PhoneCallType {
        CALL,
        PHONE_RING,
        PHONE_LISTEN,
        PHONE_HAND_UP,
        PERMISSION_DENY
    }

    public enum ScreenType {
        SCREEN_ON,
        SCREEN_OFF,
        UNLOCKED
    }

    public enum SenorType {
        SPEED_SENROR,
        DIRECT_SENSOR
    }

    public interface onBlueToothEventListener {
        void onResult(BlueToothType blueToothType, String str);
    }

    public interface onNetWorkEventListener {
        void onResult(NetworkType networkType, String str);
    }

    public interface onPhoneCallEventListener {
        void onResult(PhoneCallType phoneCallType, String str);
    }

    public interface onScreenEventListener {
        void onResult(ScreenType screenType);
    }

    public interface onSensorEventListener {
        void onSensorChanged(SensorEvent sensorEvent);
    }

    public static EvenSDK getInstance() {
        return ourInstance;
    }

    public void setOnSensorListener(Context context, final onSensorEventListener onsensoreventlistener, SenorType... senorTypeArr) {
        this.mManager = (SensorManager) context.getSystemService("sensor");
        this.mSensorEventListener = new SensorEventListener() {
            /* class com.xiaoyi.intentsdklibrary.SDK.Event.EvenSDK.AnonymousClass1 */

            public void onAccuracyChanged(Sensor sensor, int i) {
            }

            public void onSensorChanged(SensorEvent sensorEvent) {
                if (onsensoreventlistener != null) {
                    onsensoreventlistener.onSensorChanged(sensorEvent);
                }
            }
        };
        for (SenorType senorType : senorTypeArr) {
            int i = senorType.ordinal();
            if (i == 1) {
                SensorManager sensorManager = this.mManager;
                sensorManager.registerListener(this.mSensorEventListener, sensorManager.getDefaultSensor(4), 1);
            } else if (i == 2) {
                SensorManager sensorManager2 = this.mManager;
                sensorManager2.registerListener(this.mSensorEventListener, sensorManager2.getDefaultSensor(3), 1);
            }
        }
    }


    public void setOnScreenListener(Context context, final onScreenEventListener onscreeneventlistener) {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.intent.action.SCREEN_ON");
        intentFilter.addAction("android.intent.action.SCREEN_OFF");
        intentFilter.addAction("android.intent.action.USER_PRESENT");
        this.mContextScreen = context;
        BroadcastReceiver r3 = new BroadcastReceiver() {
            /* class com.xiaoyi.intentsdklibrary.SDK.Event.EvenSDK.AnonymousClass2 */

            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                action.hashCode();
                char c = 65535;
                switch (action.hashCode()) {
                    case -2128145023:
                        if (action.equals("android.intent.action.SCREEN_OFF")) {
                            c = 0;
                            break;
                        }
                        break;
                    case -1454123155:
                        if (action.equals("android.intent.action.SCREEN_ON")) {
                            c = 1;
                            break;
                        }
                        break;
                    case 823795052:
                        if (action.equals("android.intent.action.USER_PRESENT")) {
                            c = 2;
                            break;
                        }
                        break;
                }
                switch (c) {
                    case 0:
                        if (onscreeneventlistener != null) {
                            onscreeneventlistener.onResult(ScreenType.SCREEN_OFF);
                            return;
                        }
                        return;
                    case 1:
                        onScreenEventListener onscreeneventlistener2 = onscreeneventlistener;
                        if (onscreeneventlistener2 != null) {
                            onscreeneventlistener2.onResult(ScreenType.SCREEN_ON);
                            return;
                        }
                        return;
                    case 2:
                        onScreenEventListener onscreeneventlistener3 = onscreeneventlistener;
                        if (onscreeneventlistener3 != null) {
                            onscreeneventlistener3.onResult(ScreenType.UNLOCKED);
                            return;
                        }
                        return;
                    default:
                        return;
                }
            }
        };
        this.mScreenBroadcastReceiver = r3;
        this.mContextScreen.registerReceiver(r3, intentFilter);
    }

    public void setOnNetWorkListener(Context context, final onNetWorkEventListener onnetworkeventlistener) {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        this.mContextNetWork = context;
        BroadcastReceiver r3 = new BroadcastReceiver() {
            /* class com.xiaoyi.intentsdklibrary.SDK.Event.EvenSDK.AnonymousClass3 */

            public void onReceive(Context context, Intent intent) {
                ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo networkInfo = connectivityManager.getNetworkInfo(0);
                NetworkInfo networkInfo2 = connectivityManager.getNetworkInfo(1);
                if (networkInfo.isConnected() || networkInfo2.isConnected()) {
                    if (networkInfo2.isConnected() && onnetworkeventlistener != null) {
                        onnetworkeventlistener.onResult(NetworkType.NETWORK_WIFI, ((WifiManager) EvenSDK.this.mContextNetWork.getSystemService(Context.WIFI_SERVICE)).getConnectionInfo().getSSID().replace("\"", "").trim());
                    }
                    if (networkInfo.isConnected() && onnetworkeventlistener != null) {
                        onnetworkeventlistener.onResult(NetworkType.NETWORK_DATA, "");
                        return;
                    }
                    return;
                }
                onNetWorkEventListener onnetworkeventlistener2 = onnetworkeventlistener;
                if (onnetworkeventlistener2 != null) {
                    onnetworkeventlistener2.onResult(NetworkType.NETWORK_DISCONNECT, "");
                }
            }
        };
        this.mNewWrokBroadcastReceiver = r3;
        this.mContextNetWork.registerReceiver(r3, intentFilter);
    }

    public void setOnBlueToothListener(Context context, final onBlueToothEventListener onbluetootheventlistener) {
        this.mContextBluetooth = context;
        this.mBlueToothBroadcastReceiver = new BroadcastReceiver() {
            /* class com.xiaoyi.intentsdklibrary.SDK.Event.EvenSDK.AnonymousClass4 */

            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                BluetoothDevice bluetoothDevice = (BluetoothDevice) intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");
                action.hashCode();
                char c = 65535;
                switch (action.hashCode()) {
                    case -1530327060:
                        if (action.equals("android.bluetooth.adapter.action.STATE_CHANGED")) {
                            c = 0;
                            break;
                        }
                        break;
                    case -301431627:
                        if (action.equals("android.bluetooth.device.action.ACL_CONNECTED")) {
                            c = 1;
                            break;
                        }
                        break;
                    case 1821585647:
                        if (action.equals("android.bluetooth.device.action.ACL_DISCONNECTED")) {
                            c = 2;
                            break;
                        }
                        break;
                }
                switch (c) {
                    case 0:
                        int intExtra = intent.getIntExtra("android.bluetooth.adapter.extra.STATE", 0);
                        if (intExtra == 10) {
                            onBlueToothEventListener onbluetootheventlistener2 = onbluetootheventlistener;
                            if (onbluetootheventlistener2 != null) {
                                onbluetootheventlistener2.onResult(BlueToothType.BLUETOOTH_OFF, "");
                                return;
                            }
                            return;
                        } else if (intExtra == 12 && onbluetootheventlistener != null) {
                            onbluetootheventlistener.onResult(BlueToothType.BLUETOOTH_ON, "");
                            return;
                        } else {
                            return;
                        }
                    case 1:
                        onBlueToothEventListener onbluetootheventlistener3 = onbluetootheventlistener;
                        if (onbluetootheventlistener3 != null) {
                            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
                                onbluetootheventlistener3.onResult(BlueToothType.BLUETOOTH_CONNECTED, bluetoothDevice.getName());
                                return;
                            }
                         
                        }
                        return;
                    case 2:
                        onBlueToothEventListener onbluetootheventlistener4 = onbluetootheventlistener;
                        if (onbluetootheventlistener4 != null) {
                            onbluetootheventlistener4.onResult(BlueToothType.BLUETOOTH_DISCONNECTED, bluetoothDevice.getName());
                            return;
                        }
                        return;
                    default:
                        return;
                }
            }
        };
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.bluetooth.adapter.action.STATE_CHANGED");
        intentFilter.addAction("android.bluetooth.device.action.ACL_CONNECTED");
        intentFilter.addAction("android.bluetooth.device.action.ACL_DISCONNECTED");
        intentFilter.addAction("android.bluetooth.BluetoothAdapter.STATE_OFF");
        intentFilter.addAction("android.bluetooth.BluetoothAdapter.STATE_ON");
        this.mContextBluetooth.registerReceiver(this.mBlueToothBroadcastReceiver, intentFilter);
    }

    public void setOnListenScreenText(Context context, OnScreenTextListener onScreenTextListener) {
        isListenScreenText = true;
        this.mOnScreenTextListener = onScreenTextListener;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(ScreenTextBean screenTextBean) {
        OnScreenTextListener onScreenTextListener = this.mOnScreenTextListener;
        if (onScreenTextListener != null) {
            onScreenTextListener.onResult(screenTextBean);
        }
    }

    public void setOnListenNoticText(Context context, OnNoticTextListener onNoticTextListener) {
        isListenNoticText = true;
        mOnNoticTextListener = onNoticTextListener;
    }

    public void setOnKeyClickListener(OnKeyClickListener onKeyClickListener2) {
        onKeyClickListener = onKeyClickListener2;
    }

    public void onDestory() {
        try {
            onKeyClickListener = null;
            EventBus.getDefault().unregister(this);
            SensorManager sensorManager = this.mManager;
            if (sensorManager != null) {
                sensorManager.unregisterListener(this.mSensorEventListener);
            }
            Context context = this.mContextScreen;
            if (context != null) {
                context.unregisterReceiver(this.mScreenBroadcastReceiver);
            }
            Context context2 = this.mContextNetWork;
            if (context2 != null) {
                context2.unregisterReceiver(this.mNewWrokBroadcastReceiver);
            }
            Context context3 = this.mContexPhoneCall;
            if (context3 != null) {
                context3.unregisterReceiver(this.mOnPhoneCallBroadcastReceiver);
            }
            Context context4 = this.mContextBluetooth;
            if (context4 != null) {
                context4.unregisterReceiver(this.mBlueToothBroadcastReceiver);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
